package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.ResultData;
import com.eva.androidlibrary.widget.TimerWidget;
import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseTitleActivity;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.qianguo.shop.constant.ShopKeys;
import com.qianguo.shop.constant.SMSType;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * 改绑手机
 *
 * @author eva
 * @since  15/10/24
 */
@EActivity(R.layout.activity_modify_mobile)
public class ModifyMobileActivity extends BaseTitleActivity {

    @ViewById(R.id.modify_mobile_current_mobile_text_view)
    TextView mTvCurrentMobile;

    @ViewById(R.id.modify_mobile_edit_text)
    MaterialEditText mEdtNewMobile;

    @ViewById(R.id.modify_mobile_auth_code_edit_text)
    MaterialEditText mEdtAuthCode;

    @ViewById(R.id.modify_mobile_timer_widget)
    TimerWidget mTimer;

    @Extra(ShopKeys.EXTRA_KEY_USER)
    UserInfo mUser;

    public static void jumpToModifyMobile(Context context, UserInfo user) {
        Intent intent = new Intent(context, com.qianguo.shop.activity.ModifyMobileActivity_.class);
        intent.putExtra(ShopKeys.EXTRA_KEY_USER, user);
        context.startActivity(intent);
    }

    @Click(R.id.modify_mobile_done_button)
    void clickDone() {

    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.user_center_label_change_mobile);
    }

    @Override
    protected void initializeUI() {
        mTvCurrentMobile.setText(getString(R.string.modify_mobile_current, mUser.getPhone()));
        mTimer.setOnTimerListener(new TimerWidget.OnTimerListener() {
            @Override
            public void onClick() {
                String mobile = mEdtNewMobile.getText().toString();
                if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
                    mEdtNewMobile.setError(getString(R.string.edit_error_mobile));
                } else {
                    getAuthCode(mobile);
                }
            }

            @Override
            public void onTiming(int remainTime) {

            }

            @Override
            public void onFinish() {

            }
        });
    }


    /**
     * 后台请求发送短信验证码
     *
     * @param mobile
     */
    @Background
    void getAuthCode(String mobile) {
        mApp.getApi().sendSMS(SMSType.TYPE_MODIFY_MOBILE, mobile, "", new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(ModifyMobileActivity.this, msg);

            }
        });
    }
}
